home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / infptrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-03  |  10.6 KB  |  428 lines

  1. /* Low level Unix child interface to ptrace, for GDB when running under Unix.
  2.    Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "target.h"
  24.  
  25. #ifdef USG
  26. #include <sys/types.h>
  27. #endif
  28.  
  29. #include <sys/param.h>
  30. #include <sys/dir.h>
  31. #include <signal.h>
  32. #include <sys/ioctl.h>
  33. #ifndef USG
  34. #include <sys/ptrace.h>
  35. #endif
  36.  
  37. #if !defined (PT_KILL)
  38. #define PT_KILL 8
  39. #define PT_STEP 9
  40. #define PT_CONTINUE 7
  41. #define PT_READ_U 3
  42. #define PT_WRITE_U 6
  43. #define PT_READ_I 1
  44. #define    PT_READ_D 2
  45. #define PT_WRITE_I 4
  46. #define PT_WRITE_D 5
  47. #endif /* No PT_KILL.  */
  48.  
  49. #ifndef PT_ATTACH
  50. #define PT_ATTACH PTRACE_ATTACH
  51. #endif
  52. #ifndef PT_DETACH
  53. #define PT_DETACH PTRACE_DETACH
  54. #endif
  55.  
  56. #include "gdbcore.h"
  57. #ifndef    NO_SYS_FILE
  58. #include <sys/file.h>
  59. #endif
  60. #include <sys/stat.h>
  61.  
  62. #if !defined (FETCH_INFERIOR_REGISTERS)
  63. #include <sys/user.h>        /* Probably need to poke the user structure */
  64. #if defined (KERNEL_U_ADDR_BSD)
  65. #include <a.out.h>        /* For struct nlist */
  66. #endif /* KERNEL_U_ADDR_BSD.  */
  67. #endif /* !FETCH_INFERIOR_REGISTERS */
  68.  
  69.  
  70. /* This function simply calls ptrace with the given arguments.  
  71.    It exists so that all calls to ptrace are isolated in this 
  72.    machine-dependent file. */
  73. int
  74. call_ptrace (request, pid, addr, data)
  75.      int request, pid, *addr, data;
  76. {
  77.   return ptrace (request, pid, addr, data);
  78. }
  79.  
  80. #ifdef DEBUG_PTRACE
  81. /* For the rest of the file, use an extra level of indirection */
  82. /* This lets us breakpoint usefully on call_ptrace. */
  83. #define ptrace call_ptrace
  84. #endif
  85.  
  86. /* This is used when GDB is exiting.  It gives less chance of error.*/
  87.  
  88. void
  89. kill_inferior_fast ()
  90. {
  91.   if (inferior_pid == 0)
  92.     return;
  93.   ptrace (PT_KILL, inferior_pid, 0, 0);
  94.   wait ((int *)0);
  95. }
  96.  
  97. void
  98. kill_inferior ()
  99. {
  100.   kill_inferior_fast ();
  101.   target_mourn_inferior ();
  102. }
  103.  
  104. /* Resume execution of the inferior process.
  105.    If STEP is nonzero, single-step it.
  106.    If SIGNAL is nonzero, give it that signal.  */
  107.  
  108. void
  109. child_resume (step, signal)
  110.      int step;
  111.      int signal;
  112. {
  113.   errno = 0;
  114.  
  115.   /* An address of (int *)1 tells ptrace to continue from where it was. 
  116.      (If GDB wanted it to start some other way, we have already written
  117.      a new PC value to the child.)  */
  118.  
  119.   if (step)
  120. #ifdef NO_SINGLE_STEP
  121.     single_step (signal);
  122. #else    
  123.     ptrace (PT_STEP, inferior_pid, (int *)1, signal);
  124. #endif
  125.   else
  126. #ifdef AIX_BUGGY_PTRACE_CONTINUE
  127.     AIX_BUGGY_PTRACE_CONTINUE;
  128. #else
  129.     ptrace (PT_CONTINUE, inferior_pid, (int *)1, signal);
  130. #endif
  131.  
  132.   if (errno)
  133.     perror_with_name ("ptrace");
  134. }
  135.  
  136. #ifdef ATTACH_DETACH
  137. /* Nonzero if we are debugging an attached process rather than
  138.    an inferior.  */
  139. extern int attach_flag;
  140.  
  141. /* Start debugging the process whose number is PID.  */
  142. int
  143. attach (pid)
  144.      int pid;
  145. {
  146.   errno = 0;
  147.   ptrace (PT_ATTACH, pid, 0, 0);
  148.   if (errno)
  149.     perror_with_name ("ptrace");
  150.   attach_flag = 1;
  151.   return pid;
  152. }
  153.  
  154. /* Stop debugging the process whose number is PID
  155.    and continue it with signal number SIGNAL.
  156.    SIGNAL = 0 means just continue it.  */
  157.  
  158. void
  159. detach (signal)
  160.      int signal;
  161. {
  162.   errno = 0;
  163.   ptrace (PT_DETACH, inferior_pid, 1, signal);
  164.   if (errno)
  165.     perror_with_name ("ptrace");
  166.   attach_flag = 0;
  167. }
  168. #endif /* ATTACH_DETACH */
  169.  
  170. #if !defined (FETCH_INFERIOR_REGISTERS)
  171.  
  172. /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
  173.    to get the offset in the core file of the register values.  */
  174. #if defined (KERNEL_U_ADDR_BSD)
  175. /* Get kernel_u_addr using BSD-style nlist().  */
  176. CORE_ADDR kernel_u_addr;
  177.  
  178. void
  179. _initialize_kernel_u_addr ()
  180. {
  181.   struct nlist names[2];
  182.  
  183.   names[0].n_un.n_name = "_u";
  184.   names[1].n_un.n_name = NULL;
  185.   if (nlist ("/vmunix", names) == 0)
  186.     kernel_u_addr = names[0].n_value;
  187.   else
  188.     fatal ("Unable to get kernel u area address.");
  189. }
  190. #endif /* KERNEL_U_ADDR_BSD.  */
  191.  
  192. #if defined (KERNEL_U_ADDR_HPUX)
  193. /* Get kernel_u_addr using HPUX-style nlist().  */
  194. CORE_ADDR kernel_u_addr;
  195.  
  196. struct hpnlist {      
  197.         char *          n_name;
  198.         long            n_value;  
  199.         unsigned char   n_type;   
  200.         unsigned char   n_length;  
  201.         short           n_almod;   
  202.         short           n_unused;
  203. };
  204. static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
  205.  
  206. /* read the value of the u area from the hp-ux kernel */
  207. void _initialize_kernel_u_addr ()
  208. {
  209.     nlist ("/hp-ux", &nl);
  210.     kernel_u_addr = nl[0].n_value;
  211. }
  212. #endif /* KERNEL_U_ADDR_HPUX.  */
  213.  
  214. #if !defined (offsetof)
  215. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  216. #endif
  217.  
  218. /* U_REGS_OFFSET is the offset of the registers within the u area.  */
  219. #if !defined (U_REGS_OFFSET)
  220. #define U_REGS_OFFSET \
  221.   ptrace (PT_READ_U, inferior_pid, \
  222.       (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
  223. #endif
  224.  
  225. /* Registers we shouldn't try to fetch.  */
  226. #if !defined (CANNOT_FETCH_REGISTER)
  227. #define CANNOT_FETCH_REGISTER(regno) 0
  228. #endif
  229.  
  230. /* Fetch one register.  */
  231.  
  232. static void
  233. fetch_register (regno)
  234.      int regno;
  235. {
  236.   register unsigned int regaddr;
  237.   char buf[MAX_REGISTER_RAW_SIZE];
  238.   char mess[128];                /* For messages */
  239.   register int i;
  240.  
  241.   /* Offset of registers within the u area.  */
  242.   unsigned int offset;
  243.  
  244.   if (CANNOT_FETCH_REGISTER (regno))
  245.     {
  246.       bzero (buf, REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
  247.       supply_register (regno, buf);
  248.       return;
  249.     }
  250.  
  251.   offset = U_REGS_OFFSET;
  252.  
  253.   regaddr = register_addr (regno, offset);
  254.   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  255.     {
  256.       errno = 0;
  257.       *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, (int *)regaddr, 0);
  258.       regaddr += sizeof (int);
  259.       if (errno != 0)
  260.     {
  261.       sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
  262.       perror_with_name (mess);
  263.     }
  264.     }
  265.   supply_register (regno, buf);
  266. }
  267.  
  268.  
  269. /* Fetch all registers, or just one, from the child process.  */
  270.  
  271. void
  272. fetch_inferior_registers (regno)
  273.      int regno;
  274. {
  275.   if (regno == -1)
  276.     for (regno = 0; regno < NUM_REGS; regno++)
  277.       fetch_register (regno);
  278.   else
  279.     fetch_register (regno);
  280. }
  281.  
  282. /* Registers we shouldn't try to store.  */
  283. #if !defined (CANNOT_STORE_REGISTER)
  284. #define CANNOT_STORE_REGISTER(regno) 0
  285. #endif
  286.  
  287. /* Store our register values back into the inferior.
  288.    If REGNO is -1, do this for all registers.
  289.    Otherwise, REGNO specifies which register (so we can save time).  */
  290.  
  291. void
  292. store_inferior_registers (regno)
  293.      int regno;
  294. {
  295.   register unsigned int regaddr;
  296.   char buf[80];
  297.   extern char registers[];
  298.   register int i;
  299.  
  300.   unsigned int offset = U_REGS_OFFSET;
  301.  
  302.   if (regno >= 0)
  303.     {
  304.       regaddr = register_addr (regno, offset);
  305.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  306.     {
  307.       errno = 0;
  308.       ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  309.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  310.       if (errno != 0)
  311.         {
  312.           sprintf (buf, "writing register number %d(%d)", regno, i);
  313.           perror_with_name (buf);
  314.         }
  315.       regaddr += sizeof(int);
  316.     }
  317.     }
  318.   else
  319.     {
  320.       for (regno = 0; regno < NUM_REGS; regno++)
  321.     {
  322.       if (CANNOT_STORE_REGISTER (regno))
  323.         continue;
  324.       regaddr = register_addr (regno, offset);
  325.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  326.         {
  327.           errno = 0;
  328.           ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  329.               *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  330.           if (errno != 0)
  331.         {
  332.           sprintf (buf, "writing register number %d(%d)", regno, i);
  333.           perror_with_name (buf);
  334.         }
  335.           regaddr += sizeof(int);
  336.         }
  337.     }
  338.     }
  339. }
  340. #endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
  341.  
  342. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  343.    in the NEW_SUN_PTRACE case.
  344.    It ought to be straightforward.  But it appears that writing did
  345.    not write the data that I specified.  I cannot understand where
  346.    it got the data that it actually did write.  */
  347.  
  348. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  349.    to debugger memory starting at MYADDR.   Copy to inferior if
  350.    WRITE is nonzero.
  351.   
  352.    Returns the length copied, which is either the LEN argument or zero.
  353.    This xfer function does not do partial moves, since child_ops
  354.    doesn't allow memory operations to cross below us in the target stack
  355.    anyway.  */
  356.  
  357. int
  358. child_xfer_memory (memaddr, myaddr, len, write, target)
  359.      CORE_ADDR memaddr;
  360.      char *myaddr;
  361.      int len;
  362.      int write;
  363.      struct target_ops *target;        /* ignored */
  364. {
  365.   register int i;
  366.   /* Round starting address down to longword boundary.  */
  367.   register CORE_ADDR addr = memaddr & - sizeof (int);
  368.   /* Round ending address up; get number of longwords that makes.  */
  369.   register int count
  370.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  371.   /* Allocate buffer of that many longwords.  */
  372.   register int *buffer = (int *) alloca (count * sizeof (int));
  373.  
  374.   if (write)
  375.     {
  376.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  377.  
  378.       if (addr != memaddr || len < (int)sizeof (int)) {
  379.     /* Need part of initial word -- fetch it.  */
  380.         buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  381.       }
  382.  
  383.       if (count > 1)        /* FIXME, avoid if even boundary */
  384.     {
  385.       buffer[count - 1]
  386.         = ptrace (PT_READ_I, inferior_pid,
  387.               (int *)(addr + (count - 1) * sizeof (int)), 0);
  388.     }
  389.  
  390.       /* Copy data to be written over corresponding part of buffer */
  391.  
  392.       bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  393.  
  394.       /* Write the entire buffer.  */
  395.  
  396.       for (i = 0; i < count; i++, addr += sizeof (int))
  397.     {
  398.       errno = 0;
  399.       ptrace (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]);
  400.       if (errno)
  401.         {
  402.           /* Using the appropriate one (I or D) is necessary for
  403.          Gould NP1, at least.  */
  404.           errno = 0;
  405.           ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]);
  406.         }
  407.       if (errno)
  408.         return 0;
  409.     }
  410.     }
  411.   else
  412.     {
  413.       /* Read all the longwords */
  414.       for (i = 0; i < count; i++, addr += sizeof (int))
  415.     {
  416.       errno = 0;
  417.       buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  418.       if (errno)
  419.         return 0;
  420.       QUIT;
  421.     }
  422.  
  423.       /* Copy appropriate bytes out of the buffer.  */
  424.       bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  425.     }
  426.   return len;
  427. }
  428.